package cn.edu.njuit.webserver.securityserver.orders;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: TODO
 * @Data: 2022/9/19 18:20
 * @Author: guzy
 */
@RestController
@RequestMapping("/api")
public class OrderApi {
    private final UserOrderService userOrderService;
    private final OrderQueueService orderQueueService;
    private final RiderOrderService riderOrderService;

    public OrderApi(UserOrderService userOrderService,
                    OrderQueueService orderQueueService,
                    RiderOrderService riderOrderService) {
        this.userOrderService = userOrderService;
        this.orderQueueService = orderQueueService;
        this.riderOrderService = riderOrderService;
    }

    //API代码

    /**
     * API1:用户下单API
     * @param goodsName 商品名称
     * @param goodsCount 商品数量
     * @param goodsPrice 商品单价
     * @param totalPrice 订单总价
     * @param user 下单用户
     * @return
     */
    @PostMapping("/orders/create")
    public ResponseEntity createOrder(String goodsName, Long goodsCount,
                                      Long goodsPrice, Long totalPrice, String user){
        OrderEntity result=this.userOrderService.createOrder(goodsName, goodsCount, goodsPrice, totalPrice, user);
        return ResponseEntity.ok(result);
    }

    /**
     * API2:用户查询已下订单API
     * @param user
     * @return
     */
    @GetMapping("/orders/mine")
    public ResponseEntity getOrders(String user){
        List<OrderEntity> result=this.userOrderService.getMyOrders(user);
        return ResponseEntity.ok(result);
    }

    /**
     * API3:骑手获取一条配送订单API
     * @return
     */
    @GetMapping("/orders/waiting-delive")
    public ResponseEntity getDeliveOrder(){
        //
        String orderCode=this.orderQueueService.reciveDeliveOrder();
        //如果队列中没有消息
        if(orderCode==null || orderCode.isEmpty()){
            return ResponseEntity.ok("暂无待配送订单");
        }else{
            //调用骑手服务，确认配送此订单
            OrderEntity result=this.riderOrderService.confirmSend(orderCode);
            return ResponseEntity.ok(result);
        }
    }

    /**
     * API4:骑手完成配送API
     * @param orderCode
     * @return
     */
    @PostMapping("/orders/finish-delive")
    public ResponseEntity finishDelive(String orderCode){
        OrderEntity result= this.riderOrderService.finishDeliver(orderCode);
        return ResponseEntity.ok(result);
    }



}
